#!/bin/bash

#	 			Copyright 2010 Morris Blackham
#
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


#   This is the install script script to check dependencies and copy file
#   to the correct location.
#   **  More to come **
#


# set some variables and create directory structures

scriptdir="/opt/fbc"
configdir="/etc/opt/fbc"
webadmindir="/srv/www/htdocs/fbc"
dbcopy="/opt/novell/groupwise/agents/bin/dbcopy"
gwpoa="/opt/novell/groupwise/agents/bin/gwpoa"

dirs=($scriptdir $configdir $webadmindir)

for dir in ${dirs[*]}
	do
	if [ ! -d $dir ]; then
		mkdir -p $dir
	fi
done

cp ./scripts/* $scriptdir
cp -r ./webadmin/* $webadmindir

#  unremark this when fbc.xml is finalized
# cp ./config/* $configdir

#check for python and php

package=(python php)

for y in ${package[*]}

do
	#echo $y
	which $y&>/dev/null
	retval=$?

	#[ $retval = 0 ] && echo "found it"

	[ $retval != 0 ] && echo $y not found.  Please install before continuing

done

#check for apache

apache_link=/usr/sbin/httpd2
test -L $apache_link && apache_bin=$(readlink $apache_link)
retval=$?
[ $retval != 0 ] && echo Apache2 not  found.  Please install Apache before continuing

[ $retval != 0 ] && echo Apache not found.  Please install before continuing

#check for dbcopy and gwagents

if [ ! -e $dbcopy ]; then
	echo Dbcopy not found.  Please install from GroupWise Linux Distribution.
fi

if [ ! -e $gwpoa ]; then
	echo GroupWise agents not found.  Please install from GroupWise Linux Distribution.
fi

CRON_FILE=/etc/cron.d/fbc

function createcron {

   echo "Please enter time to start script.  Enter as hh:mm in 24hr format."
   read starttime

   declare -a times
   declare -i hour
   declare -i min
   times=(`echo $starttime | tr ":" " "`)
   hour=${times[0]}
   min=${times[1]}

   if [[ $hour -gt 25 ]]; then
      echo "moron,  enter a correct hour"
   fi
   if [[ $min -gt 60 ]]; then
      echo "moron,  enter valid minutes"
   fi

   echo "# fbc cron file" > $CRON_FILE
   echo "$min $hour * * * root /opt/fbc/fbc.py >> /dev/null 2>&1" >> $CRON_FILE;

}

echo "Create cron entry?"
select CRON in "Yes" "No"; do

   case $CRON in

      Yes )

         if [ -f $CRON_FILE ]; then
            echo "fbc cron file exists, do you want to edit values? y/n" yn
            select yn in "Yes" "No"; do
            case $yn in
               Yes )
                  createcron;;

               No ) 
				  break;;

            esac
         done

         else
            createcron
         fi;
         break;;

      No ) break;;
   esac
done

echo "Do you want to setup Apache to use authentication for FreebieCopy?"
select AUTH in "Yes" "No"; do

	case $AUTH in
		Yes )
			if [ -f "./SetupAuth" ]; then
				python SetupAuth
				break
			else
				echo "SetupAuth script not found"
			fi
			;;
		No ) exit;;
	esac
done


